<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <jsp:include page="/public/inc/head.jsp"/>
</head>
<body>
<div id="header" class="navbar navbar-static-top" role="navigation">
    <jsp:include page="/public/inc/top.jsp"/>
</div>
<div class="container-fluid" style="margin-top:5px;margin-bottom:60px;">
    <div class="row">
        <jsp:include page="/public/inc/menu.jsp"/>
        <div class="col-xs-12 col-sm-10">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <div class="media">
                        <div class="media-body">
                            <span class="glyphicon glyphicon-list"></span> 新增
                        </div>
                        <div class="media-right">
                            <a href="/system/wwdx_merchant/list" class="btn btn-primary">
                                <span class="glyphicon glyphicon-plus"></span> 返回
                            </a>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <form id="form1" action="/system/wwdx_merchant/cdkey_save" method="post" enctype="multipart/form-data">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="control-label col-md-2">商户</label>
                                <div class="col-md-10">
                                    <select id="merchantId" name="merchantId" class="form-control"></select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2">产品</label>
                                <div class="col-md-10">
                                    <select id="productId" name="productId" class="form-control"></select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-2">兑换码数量</label>
                                <div class="col-md-10">
                                    <input value="1" id="num" name="num" class="form-control" placeholder="必须为数字类型"/>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-floppy-o"></i> 保存
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<jsp:include page="/public/inc/bottom.jsp"/>
</body>
</html>
<script type="text/javascript">
    $(document).ready(function () {
        /**
         * 商户列表
         */
        $.ajax({
            url: "/system/wwdx_merchant/list/json",
            dataType: "json",
            success: function (data) {
                var merchantId = document.getElementById("merchantId");
                $.each(data.data, function (index, obj) {
                    //item.options.length = 0;清除所有option值
                    merchantId[0] = new Option("请选择", "", true, true);
                    merchantId[index + 1] = new Option(obj.merchant_name, obj.id);
                });
            },
            error: function (data) {
                alert(data.message);
            }
        });

        /**
         * 产品列表
         */
        $.ajax({
            url: "/system/wwdx_vip_product/list/json",
            dataType: "json",
            success: function (data) {
                var productId = document.getElementById("productId");
                $.each(data.data, function (index, obj) {
                    //item.options.length = 0;清除所有option值
                    productId[0] = new Option("请选择", "", true, true);
                    productId[index + 1] = new Option(obj.product_name, obj.product_id);
                });
            },
            error: function (data) {
                alert(data.message);
            }
        });

        $("#form1").bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                merchantId: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        }
                    }
                }, productId: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        }
                    }
                }, num: {
                    validators: {
                        notEmpty: {
                            message: '不能为空'
                        },
                        regexp: {
                            regexp: /^(\+?[1-9]\d{0,2}|\+?1000)$/,
                            message: '1-1000'
                        }
                    }
                }
            }
        });
    });
</script>